package com.menggudf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.menggudf.entity.CommonSymptom;

@Repository
public class CommonSymptomDao{
    
    private static final Log log = LogFactory.getLog(CommonSymptomDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(CommonSymptom transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(CommonSymptom persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public CommonSymptom merge(CommonSymptom detachedInstance) {
        try {
            CommonSymptom result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public CommonSymptom findById(Integer id) {
        try {
            CommonSymptom instance = entityManager.find(CommonSymptom.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    @SuppressWarnings("unchecked")
    public List<String> getCommonSymp() {
        try {
            StringBuffer sqld = new StringBuffer("select cs.symptom from CommonSymptom cs ");
            Query query = entityManager.createQuery(sqld.toString());
            return query.getResultList();
        } catch (Exception e) {
            log.error("getCommonSymp fail",e);
            throw e;
        }
    }
    
    @SuppressWarnings("unchecked")
    public CommonSymptom findBySymptom(String symptom) {
        try {
            StringBuffer sqld = new StringBuffer("from CommonSymptom cs where cs.symptom=?1 ");
            Query query = entityManager.createQuery(sqld.toString()).setParameter(1, symptom);
            List<CommonSymptom> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            log.error("findBySymptom fail",e);
            throw e;
        }
    }

    public List<CommonSymptom> getAllCommonSymptom() {
        try {
            StringBuffer sqld = new StringBuffer("from CommonSymptom");
            Query query = entityManager.createQuery(sqld.toString());
            return query.getResultList();
        } catch (Exception e) {
            log.error("getAllCommonSymptom fail",e);
            throw e;
        }
    }

    public void deleteCommonSymptomByIds(List<Integer> ids) {
        if(ids!=null && ids.size()>0){
            try {
                String idStr = ids.toString().substring(1, ids.toString().length()-1);
                String sql = "delete from CommonSymptom cs where cs.id in ("+idStr+")";
                entityManager.createQuery(sql).executeUpdate();
            } catch (RuntimeException re) {
                log.error("deleteCommonSymptomByIds failed", re);
                throw re;
            }
        }else{
            log.info("deleteCommonSymptomByIds ids is null");
        }
    }

}
